extends "res://BaseEnemy.gd"

# 森林狼特有属性
@export var leap_cooldown = 5.0  # 跳跃冷却时间
@export var leap_speed_multiplier = 2.5  # 跳跃速度倍数
@export var leap_damage_multiplier = 1.5  # 跳跃伤害倍数
@export var pack_bonus = 0.15  # 每只同伴提供的伤害加成

var leap_timer = 0.0
var can_leap = true
var is_leaping = false
var leap_target_position = Vector2.ZERO

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置森林狼特有属性
	max_health = 35
	current_health = max_health
	movement_speed = 120.0
	damage = 9
	experience_value = 7
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(0.8, 0.8, 0.8)  # 狼色调

func _physics_process(delta):
	if is_leaping:
		# 跳跃状态下的特殊移动
		var leap_direction = (leap_target_position - global_position).normalized()
		velocity = leap_direction * movement_speed * leap_speed_multiplier
		move_and_slide()
		
		# 检查是否到达目标位置
		if global_position.distance_to(leap_target_position) < 10:
			is_leaping = false
	else:
		# 非跳跃状态下执行普通的物理处理
		super._physics_process(delta)
		
		# 处理跳跃冷却
		if not can_leap:
			leap_timer += delta
			if leap_timer >= leap_cooldown:
				can_leap = true
				leap_timer = 0.0
		
		# 如果可以跳跃且玩家存在，尝试跳跃攻击
		if can_leap and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance > 50 and distance < 200:  # 在适当距离时使用跳跃攻击
				leap_attack()

func leap_attack():
	if animation_player and animation_player.has_animation("leap"):
		animation_player.play("leap")
		
		# 设置跳跃状态
		is_leaping = true
		can_leap = false
		leap_timer = 0.0
		
		# 计算目标位置（玩家位置）
		if target_player and is_instance_valid(target_player):
			leap_target_position = target_player.global_position
		
		# 等待跳跃动画完成后检查是否击中玩家
		await get_tree().create_timer(0.5).timeout
		
		if target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance <= 30:  # 如果足够接近玩家
				# 计算伤害（考虑同伴加成）
				var pack_count = count_nearby_wolves()
				var pack_damage_bonus = 1.0 + (pack_count * pack_bonus)
				var leap_damage = damage * leap_damage_multiplier * pack_damage_bonus
				
				# 对玩家造成伤害
				target_player.take_damage(leap_damage)
				
				# 击退效果
				var knockback_direction = (target_player.global_position - global_position).normalized()
				if target_player.has_method("apply_knockback"):
					target_player.apply_knockback(knockback_direction * 150)
				
				print("狼的跳跃攻击造成", leap_damage, "伤害!")
		
		print("狼发动跳跃攻击!")

# 计算附近的狼数量
func count_nearby_wolves():
	var count = 0
	var wolves = get_tree().get_nodes_in_group("enemies")
	
	for wolf in wolves:
		if wolf != self and wolf.get_script().get_path() == get_script().get_path() and is_instance_valid(wolf):
			var distance = global_position.distance_to(wolf.global_position)
			if distance <= 150:  # 只计算150单位内的狼
				count += 1
	
	return count

# 重写攻击方法
func attack_player():
	# 计算伤害（考虑同伴加成）
	var pack_count = count_nearby_wolves()
	var pack_damage_bonus = 1.0 + (pack_count * pack_bonus)
	var actual_damage = damage * pack_damage_bonus
	
	# 播放攻击动画
	if animation_player and animation_player.has_animation("attack"):
		animation_player.play("attack")
	
	# 对玩家造成伤害
	if target_player and target_player.has_method("take_damage"):
		target_player.take_damage(actual_damage)
		print("狼的攻击造成", actual_damage, "伤害!")
